import QtQuick 2.0
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12
import "../components"

Item{          // 本地文件

    id: root

    anchors.fill: parent
    anchors.margins: 15 *dp
    property int detailIdx: -1
    property int pageNum: 20
    property int page: 1


    property var listData: []

    property var filter_exp: ({
                                  "ExpName": "",
                                  "EquName": "",
                                  "ProgramName": "",
                                  "TestKit": "",

                                  "Code": "",
                                  "ConsumableType":"",
                                  "System": "",
                                  "FinishTime1": "",
                                  "FinishTime2": ""
                              })

    property var filter_data: ({
                                   "SampleName": "",
                               })

    property var letters: ["A","B","C","D","E","F","G","H","I","J","K"]

    Connections{
        target: mainwindow
        onTabHistory:{
            refresh()
        }
        onAskForTab:{
            if(mainwindow.mainPage !== 3) return
            mainwindow.permitTab()
        }
    }

    BoxSearch{
        id: boxSearch
        onAccept: {

            detailIdx = -1
            filter_exp["ExpName"]       = boxSearch.res_expName
            filter_exp["EquName"]       = boxSearch.res_equName
            filter_exp["ProgramName"]   = boxSearch.res_programName
            filter_exp["TestKit"]       = boxSearch.res_kit

            filter_exp["Code"]          = boxSearch.res_code
            filter_exp["ConsumbaleType"]= boxSearch.res_consume
            filter_exp["System"]        = boxSearch.res_system
            filter_exp["FinishTime1"]   = boxSearch.res_createTime1
            filter_exp["FinishTime2"]   = boxSearch.res_createTime2

            filter_data["SampleName"]   = boxSearch.res_sampleName

            refresh()

        }onReject: {

        }
    }

    onVisibleChanged: {
        if(!visible){
            detailIdx = -1
        }
    }

    function refresh(){

        listData = CHistory.getHistoryList(filter_exp, filter_data);
        pageNum = Math.ceil(listData.length / 20);

        // 更新页面数据
        page = 1
        updatePage()

    }



    // 翻页
    function nextPage(){
        if(page >= pageNum){
            return
        }
        page += 1

        updatePage()
    }
    function lastPage(){
        if(page <= 1){
            return
        }
        page -= 1
        updatePage()

    }
    // 页面数据根据listData刷新
    function updatePage(){
        detailIdx = -1
        var start = (page-1) * 20
        var list = layout_item.children
        for(let p = 0; p < 20; p ++){

            if(start+p >= listData.length){
                list[2*p].visible = false
                continue
            }
            list[2*p].visible = true
            list[2*p].expId = listData[start+p]["Id"]
            list[2*p].num = listData[start+p]["Idx"]
            list[2*p].expName = listData[start+p]["ExpName"]
            list[2*p].equName = listData[start+p]["EquName"]
            list[2*p].programName = listData[start+p]["ProgramName"]
            list[2*p].code = listData[start+p]["Code"]
            list[2*p].kit = listData[start+p]["TestKit"]
            list[2*p].system = listData[start+p]["System"]
            list[2*p].consume = listData[start+p]["Consume"]
            list[2*p].runTime = listData[start+p]["RunTime"]
            list[2*p].createTime = listData[start+p]["CreateTime"]
        }
    }


    function changeDetailIdx(idx, expId){
        if(idx === detailIdx){  detailIdx = -1 }
        else{ detailIdx = idx }
        var detailPannel = layout_item.children[2*idx+1]
        detailPannel.update(CHistory.getDetailList(expId, filter_data))
    }

    function tabAnalysisId(expId){
        let res = CAnalysis.openLocalAnalysis(expId)

        if(res["isOpen"]){
            mainwindow.mainPage = 2

            mainwindow.tabAnalysis(expId, res)
        }else{
            mainwindow.showToast("这里的数据走丢了，换一个实验看看吧", 1)
        }

    }


    Item{                           // 上面的条
        id: layout_top
        height: 40 *dp
        width: parent.width
        Text{
            text: "实验数据"
            font.pixelSize: config.fontsize+4
            font.bold: true
            anchors.verticalCenter: parent.verticalCenter
        }
        PushButton{
            x: parent.width - 260*dp
            title: "高级搜索"
            defaultMode2: true
            onReleased: {
                boxSearch.show()
            }
        }
        PushButton{
            x: parent.width - 120*dp
            title: "导出"
            defaultMode2: true
            onReleased: {
                let res = CHistory.datao(filter_exp, filter_data)
                if(res === 0){
                    mainwindow.showToast("导出失败", 1)
                }else if(res === 1){
                    mainwindow.showToast("导出成功", 0)
                }

            }
        }
    }
    Rectangle{      // 表头
        y: 50*dp
        height: 70*dp
        width: parent.width
        Rectangle{
            width: parent.width
            height: 10*dp
            color: "white"
        }Rectangle{
            width: parent.width
            y: 10*dp
            height: 60*dp
            color: "#F7F8FA"
            Row{
                x: 40*dp
                height: 60*dp
                Item{
                    height: 60*dp
                    width: root.width / 11 - 40*dp
                    Text {
                        text: "序号"
                        font.pixelSize: config.fontsize
                        font.bold: true
                        anchors.centerIn: parent
                    }
                }Item{
                    height: 60*dp
                    width: root.width / 11
                    Text {
                        text: "实验名称"
                        font.pixelSize: config.fontsize
                        font.bold: true
                        anchors.centerIn: parent
                    }
                }Item{
                    height: 60*dp
                    width: root.width / 11
                    Text {
                        text: "设备名称"
                        font.pixelSize: config.fontsize
                        font.bold: true
                        anchors.centerIn: parent
                    }
                }Item{
                    height: 60*dp
                    width: root.width / 11
                    Text {
                        text: "扩增程序"
                        font.pixelSize: config.fontsize
                        font.bold: true
                        anchors.centerIn: parent
                    }
                }Item{
                    height: 60*dp
                    width: root.width / 11
                    Text {
                        text: "编码"
                        font.pixelSize: config.fontsize
                        font.bold: true
                        anchors.centerIn: parent
                    }
                }Item{
                    height: 60*dp
                    width: root.width / 11
                    Text {
                        text: "试剂盒"
                        font.bold: true
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                }Item{
                    height: 60*dp
                    width: root.width / 11
                    Text {
                        text: "体系(μl)"
                        font.pixelSize: config.fontsize
                        font.bold: true
                        anchors.centerIn: parent
                    }
                }Item{
                    height: 60*dp
                    width: root.width / 11
                    Text {
                        text: "耗材"
                        font.pixelSize: config.fontsize
                        font.bold: true
                        anchors.centerIn: parent
                    }
                }Item{
                    height: 60*dp
                    width: root.width / 11
                    Text {
                        text: "运行时间"
                        font.bold: true
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                }Item{
                    height: 60*dp
                    width: root.width / 11
                    Text {
                        text: "创建时间"
                        font.pixelSize: config.fontsize
                        font.bold: true
                        anchors.centerIn: parent
                    }
                }Item{
                    height: 60*dp
                    width: root.width / 11
                    Text {
                        text: "操作"
                        font.pixelSize: config.fontsize
                        font.bold: true
                        anchors.centerIn: parent
                    }
                }
            }
        }
    }

    Rectangle{      // 列表
        id: layout_content
        x: 0
        y: 120*dp
        height: parent.height-180*dp
        width: parent.width
        //        color: "#CCCCCC"
        ScrollView{
            height: parent.height
            width: root.width + 12*dp
            clip: true
            ScrollBar.horizontal.policy: ScrollBar.AlwaysOff

            Column{
                width: root.width
                id: layout_item
                HistoryItem{
                    idx: 0
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 0
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 1
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 1
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 2
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 2
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 3
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 3
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 4
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 4
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 5
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 5
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 6
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 6
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 7
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 7
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 8
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 8
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 9
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 9
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 10
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 10
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 11
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 11
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 12
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 12
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 13
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 13
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 14
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 14
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 15
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 15
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 16
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 16
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 17
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 17
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 18
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 18
                    isVisible: detailIdx === idx && visible
                }

                HistoryItem{
                    idx: 19
                    showDetails: detailIdx === idx
                    onReleased: {
                        root.changeDetailIdx(idx, expId)
                    }
                }DetailItem{
                    idx: 19
                    isVisible: detailIdx === idx && visible
                }
            }
        }
    }

    Item{                   // 列表为空时显示图片
        id: layout_empty
        x: 0
        y: 120*dp
        height: parent.height-180*dp
        width: parent.width
        visible: listData.length === 0
        Image{
            width: 300*dp
            height: 280*dp
            anchors.centerIn: parent
            anchors.verticalCenterOffset: -80*dp
            source: "qrc:/picture/empty.png"
            Text{
                text: "还没有实验数据，快去创建实验吧~"
                font.pixelSize: config.fontsize + 2
                anchors.horizontalCenter: parent.horizontalCenter
                y: 280*dp
                color: "#AAAAAA"
            }
        }
    }

    Rectangle{      // 下面的控制按钮
        y: parent.height - 60*dp
        height: 60*dp
        width: parent.width
        Row{
            y: 15*dp
            x: 15*dp
            PushButton{
                height: 30*dp
                width: 60*dp
                title: "上一页"
                text_color: "#1677FF"
                onReleased: {
                    lastPage()
                }
            }Item{
                height: 30*dp
                width: 60*dp
                Text {
                    text: "[" + page + "/" + pageNum + "]"
                    anchors.centerIn: parent
                    font.pixelSize: config.fontsize
                }
            }
            PushButton{
                height: 30*dp
                width: 60*dp
                title: "下一页"
                text_color: "#1677FF"
                onReleased: {
                    nextPage()
                }
            }
        }
        CustomBorder{
            tBorderwidth: 1
            borderColor: "#CCCCCC"
        }

    }
}
